class News < ActiveRecord::Base
  validates :title, :presence => true
  
  belongs_to :event
  belongs_to :user
  
  belongs_to :news_status
  
  def self.editor_news_list(user, filters = {})
	@where = "user_id = '#{ user.id }' AND"

	if filters[:text] && !filters[:text].empty?
		@where += " (title LIKE '%#{ filters[:text] }%' OR" +
					" body LIKE '%#{ filters[:text] }%'" +					
					") AND"
	end
	
	if filters[:event_id] && !filters[:event_id].empty?
		@where += " event_id = '#{ filters[:event_id] }' AND"
	end

	if !@where.empty?
	  @where += " 1=1"
	end
	
	self.find(:all, :order => "news.updated_at DESC", :conditions => @where)
  end
end
